我們要先到Assets建立AR的資源才可以繼續,直接拉圖片進入Assets是不行的歐
sceneView.delegate = self
guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "ARImages", bundle: nil) else {
fatalError("無法加載參考圖像。")
}
let configuration = ARImageTrackingConfiguration()
configuration.trackingImages = referenceImages
configuration.maximumNumberOfTrackedImages = 1 // 設置同時可追蹤的最大圖像數
sceneView.session.run(configuration)
// ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
if let imageAnchor = anchor as? ARImageAnchor {
let referenceImage = imageAnchor.referenceImage
// 在這裡增加虛擬內容,例如 3D 物件或 2D 視圖
let plane = SCNPlane(width: referenceImage.physicalSize.width, height: referenceImage.physicalSize.height)
let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi / 2
node.addChildNode(planeNode)
// 給平面添加材質
plane.firstMaterial?.diffuse.contents = UIColor.red.withAlphaComponent(0.5)
}
}